import 'package:mall/constant/app_parameters.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/app_urls.dart';
import 'package:mall/modules/model/address_detail_entity.dart';
import 'package:mall/modules/model/address_entity.dart';
import 'package:mall/modules/model/coupon_entity.dart';
import 'package:mall/modules/model/foot_print_entity.dart';
import 'package:mall/modules/model/order_detail_entity.dart';
import 'package:mall/modules/model/order_list_entity.dart';
import 'package:mall/utils/broadcast/broadcast_util.dart';
import 'package:mall/utils/hud/hud_util.dart';
import 'package:mall/utils/net/http_util.dart';

//地址修改事件
abstract class AddressEvent {
  // 添加或者修改地址
  void deleteAddress(int id);
  //添加或修改了地址
  void addOrUpdateAddress();
}

//优惠券修改事件
abstract class CouponEvent {
  // 领取了优惠券
  void receiveCoupon(int couponId);
}

//反馈事件
abstract class FeedbackEvent {
  // 提交了反馈
  void submitFeedback();
}

//足迹事件
abstract class FootPrintEvent {
  // 删除了足迹
  void deleteFootPrint(int id);
}

//订单事件
abstract class OrderEvent {
  // 删除了订单
  void deleteOrder(int orderId);
  //取消了订单
  void cancelOrder(int orderId);
}

class MineService {
// 获取地址列表
  static Future<AddressEntity> getAddressList() async {
    var response = await HttpUtil.instance.get(AppUrls.ADDRESS_LIST);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return AddressEntity.fromJson(response[AppStrings.DATA]);
  }

// 查询地址详情
  static Future<AddressDetailEntity> queryAddressDetail(int addressId) async {
    var parameters = {AppParameters.ID: addressId.toString()};
    var response = await HttpUtil.instance.get(AppUrls.ADDRESS_DETAIL, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      String errorMsg =
          response[AppStrings.ERR_MSG] != null ? response[AppStrings.ERR_MSG] : AppStrings.SERVER_EXCEPTION;
      HUDUtil.toast(errorMsg);
      throw Exception(errorMsg);
    }

    return AddressDetailEntity.fromJson(response[AppStrings.DATA]);
  }

// 删除地址
  static Future<void> deleteAddress(int id) async {
    var parameters = {AppParameters.ID: id};
    var response = await HttpUtil.instance.get(AppUrls.ADDRESS_DELETE, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      String errorMsg =
          response[AppStrings.ERR_MSG] != null ? response[AppStrings.ERR_MSG] : AppStrings.SERVER_EXCEPTION;
      HUDUtil.toast(errorMsg);
      throw Exception(errorMsg);
    }
    BroadcastUtil.notify<AddressEvent>((p0) => p0.deleteAddress(id));
  }

// 添加地址
  static Future<void> addAddress({
    required String addressDetail,
    required String areaCode,
    required String city,
    required String county,
    String? id,
    bool? isDefault = false,
    required String name,
    required String province,
    required String tel,
  }) async {
    var parameters = {
      AppParameters.ADDRESS_DETAIL: addressDetail,
      AppParameters.AREA_CODE: areaCode,
      AppParameters.CITY: city,
      AppParameters.COUNTY: county,
      AppParameters.ID: id,
      AppParameters.IS_DEFAULT: isDefault,
      AppParameters.NAME: name,
      AppParameters.PROVINCE: province,
      AppParameters.TEL: tel
    };

    var response = await HttpUtil.instance.post(AppUrls.ADDRESS_SAVE, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      String errorMsg =
          response[AppStrings.ERR_MSG] != null ? response[AppStrings.ERR_MSG] : AppStrings.SERVER_EXCEPTION;
      HUDUtil.toast(errorMsg);
      throw Exception(errorMsg);
    }
    BroadcastUtil.notify<AddressEvent>((p0) => p0.addOrUpdateAddress());
  }

// 我的优惠券
  static Future<CouponEntity> queryCoupon(int pageIndex, int limit) async {
    var parameters = {AppParameters.PAGE: pageIndex, AppParameters.LIMIT: limit};
    var response = await HttpUtil.instance.get(AppUrls.MINE_COUPON_LIST, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return CouponEntity.fromJson(response[AppStrings.DATA]);
  }

// 领取优惠券
  static Future<void> receiveCoupon(int couponId) async {
    var parameters = {AppParameters.COUPON_ID: couponId};

    await HttpUtil.instance.post(
      AppUrls.RECEIVE_COUPON,
      parameters: parameters,
    );
    BroadcastUtil.notify<CouponEvent>((p0) => p0.receiveCoupon(couponId));
  }

  // 反馈
  static Future<void> feedback({
    required String content,
    required String feedBackType,
    required String phoneNumber,
  }) async {
    var parameters = {
      AppParameters.CONTENT: content,
      AppParameters.FEED_TYPE: feedBackType,
      AppParameters.HAS_PICTURE: "false",
      AppParameters.MOBILE: phoneNumber,
    };

    await HttpUtil.instance.post(
      AppUrls.FEED_BACK,
      parameters: parameters,
    );
    BroadcastUtil.notify<FeedbackEvent>((p0) => p0.submitFeedback());
  }

// 查询足迹
  static Future<FootPrintEntity> queryFootPrint(int pageIndex, int limit) async {
    var parameters = {
      AppParameters.PAGE: pageIndex,
      AppParameters.LIMIT: limit,
    };

    var response = await HttpUtil.instance.get(
      AppUrls.MINE_FOOTPRINT,
      parameters: parameters,
    );

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return FootPrintEntity.fromJson(response[AppStrings.DATA]);
  }

//删除一组足迹
  static Future<void> deleteFootPrints(List<int> ids) async {
    if (ids.length == 0) {
      HUDUtil.toast(AppStrings.PLEASE_SELECT_FOOT_PRINT);
      throw Exception(AppStrings.PLEASE_SELECT_FOOT_PRINT);
    }
    for (var id in ids) {
      await deleteFootPrint(id);
    }
  }

// 删除足迹
  static Future<void> deleteFootPrint(int id) async {
    var parameters = {AppParameters.ID: id};
    await HttpUtil.instance.post(
      AppUrls.MINE_FOOTPRINT_DELETE,
      parameters: parameters,
    );
    BroadcastUtil.notify<FootPrintEvent>((p0) => p0.deleteFootPrint(id));
  }

// 查询订单
  static Future<OrderListEntity> fetchOrders(int orderType, int pageIndex, int pageSize) async {
    var parameters = {AppParameters.SHOW_TYPE: orderType, AppParameters.PAGE: pageIndex, AppParameters.LIMIT: pageSize};
    var response = await HttpUtil.instance.get(
      AppUrls.MINE_ORDERS,
      parameters: parameters,
    );

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return OrderListEntity.fromJson(response[AppStrings.DATA]);
  }

// 删除订单
  static Future<void> deleteOrder(int orderId) async {
    var parameters = {AppParameters.ORDER_ID: orderId};
    await HttpUtil.instance.post(
      AppUrls.MINE_ORDER_DELETE,
      parameters: parameters,
    );
    BroadcastUtil.notify<OrderEvent>((p0) => p0.deleteOrder(orderId));
  }

// 取消订单
  static Future<void> cancelOrder(int orderId) async {
    var parameters = {AppParameters.ORDER_ID: orderId};
    await HttpUtil.instance.post(
      AppUrls.MINE_ORDER_CANCEL,
      parameters: parameters,
    );
    BroadcastUtil.notify<OrderEvent>((p0) => p0.cancelOrder(orderId));
  }

  // 查询订单详情
  static Future<OrderDetailEntity> fetchOrderDetail(int orderId) async {
    var parameters = {AppParameters.ORDER_ID: orderId};
    var response = await HttpUtil.instance.get(
      AppUrls.MINE_ORDER_DETAIL,
      parameters: parameters,
    );
    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }
    return OrderDetailEntity.fromJson(response[AppStrings.DATA]);
  }
}
